package com.hadluo.qq.client.ui;

import com.formdev.flatlaf.FlatDarkLaf;
import com.hadluo.qq.client.utils.Constants;

import javax.swing.*;
import java.awt.*;

/**
 * QQ管理端登录窗口
 * @author hadluo
 */
public class AdminLoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;

    public AdminLoginFrame() {
        initializeUI();
    }

    private void initializeUI() {
        // 设置窗口基本属性
        setTitle("QQ管理端登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setResizable(false);

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(new Color(35, 35, 35));

        // 创建Logo面板
        JPanel logoPanel = new JPanel();
        logoPanel.setBackground(new Color(35, 35, 35));
        JLabel logoLabel = new JLabel("QQ管理端");
        logoLabel.setFont(new Font("Arial", Font.BOLD, 48));
        logoLabel.setForeground(Color.WHITE);
        logoPanel.add(logoLabel);

        // 创建输入面板
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridBagLayout());
        inputPanel.setBackground(new Color(35, 35, 35));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 5, 10);

        // 用户名输入
        JLabel usernameLabel = new JLabel("管理员账号:");
        usernameLabel.setForeground(Color.WHITE);
        usernameField = new JTextField(20);
        styleTextField(usernameField);

        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        inputPanel.add(usernameField, gbc);

        // 密码输入
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setForeground(Color.WHITE);
        passwordField = new JPasswordField(20);
        styleTextField(passwordField);

        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        inputPanel.add(passwordField, gbc);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        buttonPanel.setBackground(new Color(35, 35, 35));

        loginButton = new JButton("登录");
        styleButton(loginButton);
        loginButton.addActionListener(e -> handleLogin());
        buttonPanel.add(loginButton);

        // 组装界面
        mainPanel.add(logoPanel, BorderLayout.NORTH);
        mainPanel.add(inputPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        setContentPane(mainPanel);
    }

    private void styleTextField(JTextField textField) {
        textField.setBackground(new Color(45, 45, 45));
        textField.setForeground(Color.WHITE);
        textField.setCaretColor(Color.WHITE);
        textField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(60, 60, 60)),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        ));
    }

    private void styleButton(JButton button) {
        button.setBackground(new Color(45, 45, 45));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(60, 60, 60)),
            BorderFactory.createEmptyBorder(5, 15, 5, 15)
        ));
    }

    private void handleLogin() {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());

        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this,
                "管理员账号和密码不能为空",
                "错误",
                JOptionPane.ERROR_MESSAGE);
            return;
        }

        // TODO: 实现管理员登录验证逻辑
        // 验证成功后打开管理主界面
        openAdminMainFrame();
    }

    private void openAdminMainFrame() {
        // TODO: 打开管理主界面
        dispose(); // 关闭登录窗口
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new FlatDarkLaf());
        } catch (Exception e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(() -> {
            AdminLoginFrame loginFrame = new AdminLoginFrame();
            loginFrame.setVisible(true);
        });
    }
}